package com.cyandpos.pad.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class TimeUtils {

    public static String FORMAT_ONE = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_TWO = "yyyy年MM月dd日 HH时mm分ss秒";
    public static String FORMAT_TWO_2 = "yyyy年MM月dd日 HH时mm分";
    public static String FORMAT_THREE = "yyyy/MM/dd HH:mm:ss";
    public static String FORMAT_FOUR = "yyyyMMddHHmmss";

    /**
     * @return 返回calendar的时间
     */
    public static String converCalendar2String(Calendar calendar) {
        return converCalendar2StringWithFormat(calendar, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * @return返回calendar的时间
     */
    public static String converCalendar2StringWithFormat(Calendar calendar, String formt) {
        return new SimpleDateFormat(formt).format(calendar.getTime());
    }

    /**
     * @param date 把Date转化成Calendar
     * @return
     */
    public static Calendar dateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }


    /**
     * 把Calendar转化成Date
     *
     * @param calendar
     * @return
     */
    public static Date calendarToDate(Calendar calendar) {
        return calendar == null ? null : calendar.getTime();
    }

    /**
     * 获取格式时间到秒
     */
    public static String getCurrentTime() {
        String formt = "yyyy-MM-dd HH:mm:ss";
        return new SimpleDateFormat(formt).format(Calendar.getInstance().getTime());
    }


    /**
     * 获取格式时间到天
     */
    public static String getCurrentDate() {
        String formt = "yyyy-MM-dd";
        return new SimpleDateFormat(formt).format(Calendar.getInstance().getTime());
    }

    /**
     * 将时间字符串截取为日期字符串yyyy/mm/dd
     *
     * @param time
     * @return
     */
    public static String subString(String time) {
        String date = time.substring(0, 10);
        return date;
    }


    /**
     * 将STRING类型的时间从一个format转到另一个
     *
     * @param time
     * @return
     */
    public static String transFormatToFormant(String time, String format_in, String format_out) {
        String result = null;
        try {
            Date parse = new SimpleDateFormat(format_in).parse(time);
            result = new SimpleDateFormat(format_out).format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return result;
    }


    /**
     * date2比date1多的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static int differentDays(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) // 同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) // 闰年
                {
                    timeDistance += 366;
                } else // 不是闰年
                {
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else // 不同年
        {
            return day2 - day1;
        }
    }




}
